package com.panfeng.xcloud.boss.provider.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.panfeng.xcloud.boss.provider.member.dao.mapper.NoticeMsgInfoMapperExt;
import com.panfeng.xcloud.boss.provider.member.dao.mapper.UserNoticeMsgRelationMapperExt;
import com.panfeng.xcloud.boss.provider.member.dto.request.AddNoticeRelationReqDTO;
import com.panfeng.xcloud.boss.provider.member.dto.request.NoticeMsgReqDTO;
import com.panfeng.xcloud.boss.provider.member.dto.response.NoticeMsgInfoItemRespDTO;
import com.panfeng.xcloud.boss.provider.member.dto.response.NoticeMsgPageRespDTO;
import com.panfeng.xcloud.boss.provider.member.service.INoticeService;
import com.panfeng.xcloud.common.core.page.PageResponse;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.dao.base.MapperSupport;
import com.panfeng.xcloud.dao.member.entity.NoticeMsgInfo;
import com.panfeng.xcloud.dao.member.entity.UserNoticeMsgRelation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class NoticeServiceImpl extends MapperSupport<Integer, NoticeMsgInfo, NoticeMsgInfoMapperExt> implements INoticeService {

    @Autowired
    private NoticeMsgInfoMapperExt noticeMsgInfoMapperExt;

    @Autowired
    private UserNoticeMsgRelationMapperExt userNoticeMsgRelationMapperExt;

    @Override
    public PageResponse<NoticeMsgPageRespDTO> queryNoticeMsg(NoticeMsgReqDTO noticeMsgReqDTO) {
        PageResponse<NoticeMsgInfoItemRespDTO> pageResponse = null;
        if(noticeMsgReqDTO.getUserId() == null){
            pageResponse = super.selectPage(noticeMsgReqDTO, noticeMsgInfoMapperExt::queryNoticeMsgInfoPageCount, noticeMsgInfoMapperExt::queryNoticeMsgInfoPage);
        }else{
            pageResponse = super.selectPage(noticeMsgReqDTO, noticeMsgInfoMapperExt::queryNoticeMsgPageCount, noticeMsgInfoMapperExt::queryNoticeMsgPage);
        }

        log.info(">>>> 查询pageResponse QuantBot头条信息:{}<<< ",(null != pageResponse? JSON.toJSONString(pageResponse):""));

        List<NoticeMsgInfoItemRespDTO> items = pageResponse.getItems();
        List<NoticeMsgPageRespDTO> finalResponse = ConvertUtils.convert(items, NoticeMsgPageRespDTO.class);

        log.info(">>>> 查询finalResponse QuantBot头条信息:{}<<< ",(null != finalResponse? JSON.toJSONString(finalResponse):""));

        PageResponse<NoticeMsgPageRespDTO> pageFinalResponse = new PageResponse();
        pageFinalResponse.setTotalCount(pageResponse.getTotalCount());
        pageFinalResponse.measureTotalPage(pageResponse.getTotalCount(),pageResponse.getPageSize());
        pageFinalResponse.setItems(finalResponse);
        pageFinalResponse.setPageNumber(noticeMsgReqDTO.getPageNumber());
        pageFinalResponse.setPageSize(pageResponse.getPageSize());

        log.info(">>>> 查询pageFinalResponse QuantBot头条信息:{}<<< ",(null != pageFinalResponse? JSON.toJSONString(pageFinalResponse):""));

        return pageFinalResponse;
    }

    @Override
    public boolean addUserNoticeRelation(AddNoticeRelationReqDTO reqBean) {
        UserNoticeMsgRelation userNoticeMsgRelation = new UserNoticeMsgRelation();
        userNoticeMsgRelation.setNoticeMsgId(reqBean.getNoticeMsgId());
        userNoticeMsgRelation.setUserId(reqBean.getUserId());
        userNoticeMsgRelation.setReadTime(new Date());
        int rs = userNoticeMsgRelationMapperExt.insertSelective(userNoticeMsgRelation);

        if(rs > 0){
            log.info(">>> 添加消息公告消息关系成功 <<<");
        }else{
            log.info(">>> 添加消息公告消息关系失败 <<<");
        }

        return rs > 0;
    }

}
